﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickApp.Core;
using EfSearchModel.Model;
using QuickApp.MVCControl;
using Resto.CostControl.Core;
using QuickApp.MVCControl.Form;
using Resto.CostControl.Core.Models;

namespace Resto.CostControl.Web.Controllers
{
    public class DishController : Controller
    {
        private readonly IRepository<Dish> _Repo;
        private readonly IRepository<DishMaterial> _RepoDM;
        private readonly IRepository<Material> _RepoM;
        private readonly IRepository<DishRange> _RepoRang;
        private readonly IUnitOfWork _UnitOfWork;

        public DishController(IRepository<Dish> repo, IRepository<DishMaterial> repoDM, IRepository<Material> RepoM, IRepository<DishRange> RepoRang, UnitOfWork unitOfWork)
        {
            _Repo = repo;
            _RepoDM = repoDM;
            _RepoM = RepoM;
            _RepoRang = RepoRang;
            _UnitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Index(DWZPageInfo pageInfo, DWZQueryModel where)
        {
            var list = _Repo.GetPageable(pageInfo.ToPageInfo(), where);
            var fdata = pageInfo.ToFlexiGridData<Dish>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }

        public ActionResult Edit(string Id)
        {
            Dish data = null;
            if (!string.IsNullOrWhiteSpace(Id))
            {
                data = _Repo.Find(Id);
            }
            else
            {
                data = new Dish();
                data.Id = Guid.NewGuid().ToString();
            }
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(Dish data, IList<DishMaterial> MaterialList)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            if (ModelState.IsValid)
            {
                try
                {
                    if (_Repo.Find(data.Id) == null)
                    {
                        _Repo.Insert(data);
                    }
                    else
                    {
                        data = _Repo.Find(data.Id);
                        TryUpdateModel(data);

                        _RepoDM.DeleteAll(m => m.DishId == data.Id);
                    }

                    if (MaterialList != null)
                    {
                        foreach (var m in MaterialList)
                        {
                            m.DishId = data.Id;
                            m.MaterialId = m.Id;
                            m.Id = Guid.NewGuid().ToString();

                            _RepoDM.Insert(m);
                        }
                    }

                    _UnitOfWork.Commit();
                    msg.IsSuccess = true;
                    msg.Msg = "操作成功";
                }
                catch (Exception ex)
                {
                    msg.IsSuccess = false;
                    msg.Msg = "操作失败：" + ex.Message;
                }
            }
            return Json(msg);
        }

        [HttpPost]
        public JsonResult Delete(string id)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            if (!string.IsNullOrEmpty(id))
            {
                try
                {
                    _Repo.Delete(id);
                    _UnitOfWork.Commit();
                    msg.IsSuccess = true;
                    msg.Msg = "操作成功";
                }
                catch
                {
                    msg.IsSuccess = false;
                    msg.Msg = "操作失败,请稍后重试！";
                }
            }
            else
            {
                msg.IsSuccess = false;
                msg.Msg = "参数错误";
            }
            return Json(msg);
        }

        [HttpPost]
        public JsonResult DeleteItem(string Id)
        {
            JsonReturnMessages msg = new JsonReturnMessages();
            if (!string.IsNullOrEmpty(Id))
            {
                try
                {
                    _RepoDM.Delete(Id);
                    _UnitOfWork.Commit();
                    msg.IsSuccess = true;
                    msg.Msg = "操作成功";
                }
                catch
                {
                    msg.IsSuccess = false;
                    msg.Msg = "操作失败,请稍后重试！";
                }
            }
            else
            {
                msg.IsSuccess = false;
                msg.Msg = "参数错误";
            }
            return Json(msg);
        }

        [HttpPost]
        public JsonResult SaveItem(DishMaterial data)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(data.Id))
                    {
                        data.Id = Guid.NewGuid().ToString();
                        _RepoDM.Insert(data);
                    }
                    else
                    {
                        data = _RepoDM.Find(data.Id);
                        TryUpdateModel(data);
                    }

                    _UnitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            var result = new JsonReturnMessages
            {
                IsSuccess = true,
                Msg = "操作成功",
            };
            return Json(result);
        }

        public ActionResult EditItem(string Id, string parentId)
        {
            DishMaterial data = null;
            if (!string.IsNullOrWhiteSpace(Id))
            {
                data = _RepoDM.Find(Id);
            }
            else
            {
                data = new DishMaterial();
                data.DishId = parentId;
            }
            return View(data);
        }

        public ActionResult LookUp()
        {
            return View();
        }

        public ActionResult LookupDish(string Date)
        {
            ViewBag.Date = Date;
            return View();
        }

        public ActionResult MultipleLookup()
        {
            return View();
        }

        [HttpPost]
        public ActionResult MultipleDishList(DWZPageInfo pageInfo, DWZQueryModel where)
        {
            var list = _Repo.GetPageable(pageInfo.ToPageInfo(), where);
            var fdata = pageInfo.ToFlexiGridData<Dish>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }

        [HttpPost]
        public ActionResult DishList(string Date, DWZPageInfo pageInfo, DWZQueryModel where)
        {
            where.AddItem("Date", Date, QueryMethod.DateBlock);
            var list = _RepoRang.GetPageable(pageInfo.ToPageInfo(), where);
            var fdata = pageInfo.ToFlexiGridData<DishRange>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }

        [HttpPost]
        public JsonResult MaterialList(DWZPageInfo pageInfo, DWZQueryModel where)
        {
            var list = _RepoM.GetPageable(pageInfo.ToPageInfo(), where);
            var fdata = pageInfo.ToFlexiGridData<Material>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }

        public ActionResult EditList()
        {
            return View();
        }

        [HttpPost]
        public JsonResult EditList(string parentId, DWZPageInfo pageInfo, DWZQueryModel where)
        {
            where.AddItem("DishId", parentId == "" ? null : parentId);
            var list = _RepoDM.GetPageable(pageInfo.ToPageInfo(), where);
            var fdata = pageInfo.ToFlexiGridData<DishMaterial>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }
    }
}